#
#  untitled.rb
#  
#
#  Created by Zhang Ximin on 8/4/10.
#  Copyright (c) 2010 Columbia University. All rights reserved.
#

require 'rubygems'
require 'watir'
require 'test/unit'
require 'test/unit/testsuite'
#require 'safariwatir'
require 'mysql'
require 'C:\Watir_test\shermanstravelmedia\environment\html_class_v2\classes\html.class'
require 'resolv'
require 'test/unit/ui/console/testrunner'
require 'watir/contrib/enabled_popup'

# initialization


begin


# ====================CLASS DEFINITION=========================





# ---------------------TEST GENERAL CLASS----------------------
class TG_KLASS < Test::Unit::TestCase
	#attr_accessor :browser_type, :url, :environment			
	def setup
		$browser_type = 'ie'
		#Watir::Browser.default= $browser_type
		$environment = 'QA'
		$flag_lux = true
		$flag_top25 = true
		#$html = HTMLReport.new()
		#$reportRes = $html.createReport('STM', 'Shermans Travel Media Test Report', $browser_type)
	end	
	
	def test_dbConn
		$my = Mysql.new("db1.shermanstravel.com", "webuser", "0xQ!=_7R", 'st')
	end
	# Success
	def test_emailVal(email, task)
		@email = email
		@test_task = task
		@valid = true
		unless @email.nil?
				unless @email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
				# email address is invalid
					$html.add_to_report(false, @test_task, 'Warning! Email address '+@email+' does not appear to be valid!', 'Warning! Email address ('+@email+') does not appear to be valid!')
					@valid = false
				else
				# email address is valid without domain check
					@valid = true
					$html.add_to_report(true, @test_task, 'Email address ('+@email+') is valid!', 'Email address ('+@email+') is valid!')
				end
		end
		@valid
	end
	
	def teardown
		# Do nothing here
	end
end

# ----------------------REPORT CLASS--------------------------

class HTML_report
	attr_accessor :name, :header, :browser_type	
	def test_init(name, header, browser_type)
		@name = name
		@header = header
		@browser_type = $browser_type
	end
	
	def test_initReport
		$html = HTMLReport.new()
		$report = $html.createReport(@name, @header, @browser_type)
	end
	
end


# ~~TEST FOR LUXURY
class Luxury < TG_KLASS
	
	def setup
		Watir::Browser.default= $browser_type
		@site = "http://www.shermanstravel.com/luxury"
	end
	
	def test_00_start
		$browser = Watir::Browser.start(@site)
		$email = 'zhangxmbxy@_tmail.com'
		test_case = $html.newTestName('Verify Luxury')
		$email_valid = false
	end

	def test_01_emailVal
		$lux_email_valid = self.test_emailVal($email, "Email validation check")
	end
	
#	def test_010_popupChecker(text)
#	    Timeout::timeout(2)do
#	        begin
#	            if $browser.enabled_popup
#	                hwnd = $browser.enabl#ed_popup(5)
#	                w = WinClicker.new
#	                w.makeWindowActive(hwnd)
#	                w.clickWindowsButton_hWnd(hwnd,text)
#	            end
#	        rescue Timeout::Error
#	            puts 'No popup existed'
#	        end
#	    end
#	end
	
#	def test_011_alert
#		#$browser.button(:text, 'Continue').click_no_wait

		# Use click_no_wait to launch the popup or your script will hang
#		$browser.button(:src,"http://i.stcdn.net/btn_signmeup_light_orange.jpg").click

#		self.test_010_popupChecker('OK')
#		$browser.wait
		#if (hwnd)  #yeah! a popup
		#  popup = WinClicker.new
		#  popup.makeWindowActive(hwnd)
		#  popup.clickWindowsButton("Windows Internet Explorer", "OK", "30")
		#end
#	end

	
	def test_02_simulate
		#if ($lux_email_valid)
			$btn_click = false
			@lux_load = $browser.text_field(:id, "txtNewsletterSignupEmail").exists?
			$html.add_to_report(@lux_load, 'Luxury page loading check', 'Page loads successfully', 'Page fails to load')
		if ($lux_email_valid && @lux_load)
			$browser.text_field(:id, "txtNewsletterSignupEmail").set($email)
			$browser.button(:src,"http://i.stcdn.net/btn_signmeup_light_orange.jpg").click
			@lux_detail_load = $browser.text_field(:id, "lux_my_zip").exists?
			$html.add_to_report(@lux_detail_load, 'Luxury/details page loading check', 'Page loads successfully', 'Page fails to load')
			if (@lux_detail_load)			
				$browser.text_field(:id, "lux_my_zip").set("10025")
				$browser.checkbox(:id, "si_1").set
				$browser.checkbox(:id, "si_9").set
				$browser.text_field(:id, "lux_first_name").set("Ximin")
				$browser.text_field(:id, "lux_last_name").set("zhang")
				$browser.text_field(:id, "lux_city").set("ny")
				$browser.text_field(:id, "lux_addr_two").set("APT 3e")
				$browser.text_field(:id, "lux_addr_one").set("219 W 100")
				if $browser_type == 'safari'
				$browser.select_list(:id, "people_address_state_or_territory").select("NJ")
				$browser.select_list(:id, "people_address_state_or_territory").select("NY")
				$browser.select_list(:id, "people_address_country_id").select("Tuvalu")
				$browser.select_list(:id, "people_address_country_id").select("United States")
				$browser.text_field(:id, "lux_zip").set("10024")
				$browser.text_field(:id, "lux_phone").set("347-226-1816-1")
				$browser.text_field(:id, "lux_phone").set("347-226-1816")
				$browser.checkbox(:id, "magazine_checkbox").set
				$browser.button(:value,"Submit").click
				else
				$browser.select_list(:id, "people_address_state_or_territory").set("NJ")
				$browser.select_list(:id, "people_address_state_or_territory").set("NY")
				$browser.select_list(:id, "people_address_country_id").set("Tuvalu")
				$browser.select_list(:id, "people_address_country_id").set("United States")
				$browser.text_field(:id, "lux_zip").set("10024")
				$browser.text_field(:id, "lux_phone").set("347-226-1816-1")
				$browser.text_field(:id, "lux_phone").set("347-226-1816")
				$browser.checkbox(:id, "magazine_checkbox").set
				$browser.button(:value,"Submit").click				
				end
				$btn_click = true
				puts $btn_click
				#@lux_detail_redir = $browser.text_field(:name, "FirstName").exists?
				#$html.add_to_report(@lux_detail_redir, 'Luxury/details page redirection check', 'Page redirects successfully', 'Page fails to redirect')				
			end
		end
	end
	
	def test_03_email_store
		puts $btn_click
		$lux_email_store = false
		if ($lux_email_valid && $btn_click)
			$res = $my.query("SELECT * FROM people where email = '"+$email+"' " )  
			# report generation
			if($res)
				$lux_email_store = true
			end
			$html.add_to_report(!$res.nil?, 'Email Storage Check', 'Email('+$email+')  stored successfully or exists before', 'Unable to store email address '+$email+' ')
		end
	end
	
	def test_04_zipcode_store
		$lux_post_store = false
		if ($lux_email_store)
			res3 = $my.query("SELECT pa.id FROM people p, people_addresses pa where pa.id=p.address_id and p.email= '"+$email+"' ")
			res4 = $my.query("SELECT pa.postal_code FROM people p, people_addresses pa where pa.id=p.address_id and p.email= '"+$email+"' ")
			if(res3) # The user has submitted
				if (res4)
					$lux_post_store = true
				end
				$html.add_to_report(!res4.nil?, 'Postal code Storage Check', 'Postal code corresponding to ('+$email+') has been stored successfully(or exists before) ', 'Unable to store postal code with '+$email+' ')
			else # No submit of the details, no instance
				$html.add_to_report(false, 'Postal code Storage Check', 'No instance of ('+$email+') in people_address table', 'No instance of ('+$email+') in people_address table')
			end
		end
	end

	def teardown
		if ($browser_type == 'firefox')
			#$browser.close
		end
	end
	
	
end

# ~~TEST FOR LUXURY
class Top25 < TG_KLASS
	def setup
		Watir::Browser.default= $browser_type
		@site = "http://www.shermanstravel.com/top25/signup"
		@email = "xmbxy@hotmail.com"
	end
	
	def test_00_start
		$browser = Watir::Browser.start(@site)
		test_case = $html.newTestName('Verify Top25')
	end
	
	def test_01_emailVal
		$top25_email_valid = self.test_emailVal(@email, "Email validation check")		
	end
	
	puts $top25_email_valid

	def test_02_simulate
		if ($top25_email_valid)
			$browser.text_field(:id, "primary_email").set(@email)
			$browser.text_field(:id, "postal_code").set("10025")
			$browser.button(:src,"http://i.stcdn.net/top25/new_signup/subscribe.jpg").click
			puts Watir::IE.find(:url, "http://www.shermanstravel.com/top25/thanks")
		end
	end
	
	def test_03_email_store
		$top25_email_store = false
		if ($top25_email_valid)
			$res = $my.query("SELECT * FROM people where email = '"+@email+"' " )  
			# report generation
			if($res)
				$top25_email_store = true
			end
			$html.add_to_report(!$res.nil?, 'Email storage check', 'Email ('+@email+') stored successfully or email exists before', 'Unable to store email address ('+@email+')')
		end
	end
	
	def test_04_zipcode_store
		$top25_post_store = false
		if ($top25_email_store)
			res3 = $my.query("SELECT pa.id FROM people p, people_addresses pa where pa.id=p.address_id and p.email= '"+@email+"' ")
			res4 = $my.query("SELECT pa.postal_code FROM people p, people_addresses pa where pa.id=p.address_id and p.email= '"+@email+"' ")
			if(res3) # The user has submitted
				if (res4)
					$top25_post_store = true
				end
				$html.add_to_report(!res4.nil?, 'Postal code Storage Check', 'Postal code corresponding to ('+@email+') has been stored successfully(or exists before) ', 'Unable to store postal code with '+@email+' ')
			else # No submit of the details, no instance
				$html.add_to_report(false, 'Postal code Storage Check', 'No instance of ('+@email+') in people_address table', 'No instance of ('+@email+') in people_address table')
			end
		end
	end
	
	def teardown
	#@browser.close
	end
	
end


class FinalReport
	
	def finish
		$html.finishReport($report, $browser_type, $environment)
	end
	
end



# =========================EXCECUTION===========================
Test::Unit::UI::Console::TestRunner.run(TG_KLASS)
@instance = HTML_report.new
@instance.test_init('STM', 'Shermans Travel Media Test Report', $browser_type)
@instance.test_initReport
Test::Unit::UI::Console::TestRunner.run(Luxury)
Test::Unit::UI::Console::TestRunner.run(Top25)
@instance = FinalReport.new
@instance.finish



end

